package com.rocky.project.mvc;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.rocky.project.service.ProjectInitService;
import com.rocky.project.util.WebUtil;


public class WebApplicationInitListener implements ServletContextListener {
	private ServletContext servletContext;
	private WebApplicationContext webApplicationContext;
	private final static Logger log = Logger.getLogger(WebApplicationInitListener.class);
	public void contextDestroyed(ServletContextEvent sce) {
		WebUtil.saveConfig();
        if (log.isDebugEnabled())
            log.debug("WebApplicationInitListener is destroying........");		
	}
	public void contextInitialized(ServletContextEvent sce) {
        this.servletContext = sce.getServletContext();
        webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ProjectInitService projectInitService = (ProjectInitService) webApplicationContext.getBean("projectInitService");
        try {
			projectInitService.initData();
		} catch (Exception e) {
			log.error("#WebApplicationInitListener->contextInitialized#:"+e.getMessage());
		}		
	}

}
